package com.oro.util;

import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.jsf.FacesContextUtils;

public class FacesUtils {

	public static void addErrorMessage(String msg) {
		addMessage(FacesMessage.SEVERITY_ERROR, msg);
	}

	private static void addMessage(FacesMessage.Severity severity, String msg) {
		final FacesMessage facesMsg = new FacesMessage(severity, msg, msg);
		FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}

	public static void addSuccessMessage(String msg) {
		addMessage(FacesMessage.SEVERITY_INFO, msg);
	}

	public static String getBundleKey(String bundleName, String key) {
		return FacesContext
				.getCurrentInstance()
				.getApplication()
				.getResourceBundle(FacesContext.getCurrentInstance(),
						bundleName).getString(key);
	}

	public static String getBundleKey(String key) {
		return ResourceBundle.getBundle(
				FacesContext.getCurrentInstance().getApplication()
						.getMessageBundle(),
				FacesContext.getCurrentInstance().getViewRoot().getLocale())
				.getString(key);
	}

	public static Object getLoggedUser() {
		Object loggedUser = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		return loggedUser;
	}

	public static Object getBean(String beanName) {
		return FacesContextUtils.getWebApplicationContext(
				FacesContext.getCurrentInstance()).getBean(beanName);
	}

}